package com.gexiaobao.pigeon.ui.fragment.mall

import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.gexiaobao.pigeon.R
import com.gexiaobao.pigeon.app.base.BaseFragment
import com.gexiaobao.pigeon.app.util.StatusBarUtil
import com.gexiaobao.pigeon.databinding.FragmentMineOrderBinding
import com.gexiaobao.pigeon.ui.mall.MallViewModel
import com.google.android.material.tabs.TabLayout
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.util.setOnclickNoRepeat

/**
 * @Author      : hxw
 * @Date        : 2023/6/20 14:31
 * @Describe    : 我的订单
 */
class FragmentMineOrder : BaseFragment<MallViewModel, FragmentMineOrderBinding>() {

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        activity?.let { StatusBarUtil.setDarkMode(it) }
        activity?.let { StatusBarUtil.setColor(it, Color.TRANSPARENT, 0) }
    }

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.includeBar.tvWhiteTitle.text = "我的订单"
        initViewPager()
    }

    private fun initViewPager() {
        val orderIndex = arguments?.getInt("orderIndex", 0)
        val tab1 = mDatabind.tabLayout.newTab().setText(getString(R.string.market_shop_order_list_for_fragment_layout_activity_my_order_tab_all))
        mDatabind.tabLayout.addTab(tab1)
        val tab2 = mDatabind.tabLayout.newTab().setText(getString(R.string.market_shop_order_list_for_fragment_layout_activity_my_order_tab_unpay))
        mDatabind.tabLayout.addTab(tab2)
        val tab3 =
            mDatabind.tabLayout.newTab().setText(getString(R.string.market_shop_order_list_for_fragment_layout_activity_my_order_tab_unrecieve))
        mDatabind.tabLayout.addTab(tab3)
        val tab4 = mDatabind.tabLayout.newTab().setText(getString(R.string.market_shop_order_list_for_fragment_layout_activity_my_order_tab_complete))
        mDatabind.tabLayout.addTab(tab4)
        val tab5 = mDatabind.tabLayout.newTab().setText("已取消")
        mDatabind.tabLayout.addTab(tab5)

        mDatabind.tabLayout.addOnTabSelectedListener(object : TabLayout.BaseOnTabSelectedListener<TabLayout.Tab> {
            override fun onTabReselected(p0: TabLayout.Tab) {}
            override fun onTabUnselected(p0: TabLayout.Tab) {}

            override fun onTabSelected(p0: TabLayout.Tab) {
                replaceFragments(p0.position)
            }
        })
        if (orderIndex != null) {
            replaceFragments(orderIndex)
            mDatabind.tabLayout.getTabAt(orderIndex)!!.select()
        }
    }

    override fun onBindViewClick() {
        super.onBindViewClick()
        setOnclickNoRepeat(mDatabind.includeBar.ivBack) {
            nav().navigateUp()
        }
    }

    private fun replaceFragments(position: Int) {
        childFragmentManager.beginTransaction().apply {
            when (position) {
                0 -> replace(R.id.home_activity_frag_container, FragmentAllShopOrderList.newInstance(0))//全部订单
                1 -> replace(R.id.home_activity_frag_container, FragmentShopOrderList.newInstance(1))//等待支付
                2 -> replace(R.id.home_activity_frag_container, FragmentReceiveOrderList.newInstance(3))//等待收货
                3 -> replace(R.id.home_activity_frag_container, FragmentReceiveOrderList.newInstance(4))//等待收货
                4 -> replace(R.id.home_activity_frag_container, FragmentShopOrderList.newInstance(5))//已取消
            }
        }.commitAllowingStateLoss()
    }
}